package descarte.com.br.descarte;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.ByteArrayOutputStream;

/**
 * Created by gdss on 22/03/2015.
 */
public class NovaReciclagemActivity2 extends FragmentActivity {

    SupportMapFragment map;
    GoogleMap mapa;
    Marker marker;
    ImageButton imgButton;
    byte[] byteArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nova_reciclagem2);

        ActionBar actionBar = getActionBar();
        actionBar.setTitle(PropertiesUtils.getInscance().getCategoria());
        actionBar.setDisplayHomeAsUpEnabled(true);

        Button btGerar = (Button) findViewById(R.id.btGerar);
        btGerar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // salvar a reciclagem no banco
            }
        });

        imgButton = (ImageButton) findViewById(R.id.btCamera);

        GoogleMapOptions options = new GoogleMapOptions();
        options.zOrderOnTop(true);

        map = SupportMapFragment.newInstance(options);
        android.support.v4.app.FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
        trans.replace(R.id.fragmetMapa, map);
        trans.commit();

        imgButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                open();
            }
        });
    }

    public void open() {
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        Bitmap bp = (Bitmap) data.getExtras().get("data");

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bp.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byteArray = stream.toByteArray();
        byteArray = Base64.encode(byteArray, Base64.DEFAULT);


        imgButton.setImageBitmap(bp);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Thread() {
            public void run() {
                while (map.getMap() == null) {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {

                    }
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        configMap();
                    }
                });
            }
        }.start();
    }

    public void configMap() {
        mapa = map.getMap();
        mapa.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mapa.setMyLocationEnabled(true);


        LatLng latNg = new LatLng(-8.1192006, -34.9049338);

        CameraPosition posistion = new CameraPosition.Builder().target(latNg).zoom(15).build();
        CameraUpdate update = CameraUpdateFactory.newCameraPosition(posistion);


        //mapa.moveCamera(update);
        mapa.animateCamera(update, 3000, new GoogleMap.CancelableCallback() {
            @Override
            public void onFinish() {

            }

            @Override
            public void onCancel() {

            }
        });


        mapa.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                if (marker != null) {
                    marker.remove();
                }

                addMarck(new LatLng(latLng.latitude, latLng.longitude), "Seu Endereço", "Click para salvar");
            }
        });


        mapa.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
            @Override
            public boolean onMyLocationButtonClick() {
                if (marker != null) {
                    marker.remove();
                }

                addMarck(new LatLng(mapa.getMyLocation().getLatitude(), mapa.getMyLocation().getLongitude()), "Seu Endereço", "Click para salvar");


                return false;
            }
        });

    }

    public void addMarck(LatLng lt, String titule, String snipet) {
        MarkerOptions options = new MarkerOptions();
        options.position(lt).title(titule).snippet(snipet).draggable(true);

        if (PropertiesUtils.getInscance().getTipo().equals(PropertiesUtils.TipoReciclado.METAIS)) {
            options.icon(BitmapDescriptorFactory.fromResource(R.drawable.pino_metal));
        } else if (PropertiesUtils.getInscance().getTipo().equals(PropertiesUtils.TipoReciclado.PAPÉIS)) {
            options.icon(BitmapDescriptorFactory.fromResource(R.drawable.pino_papel));
        } else if (PropertiesUtils.getInscance().getTipo().equals(PropertiesUtils.TipoReciclado.PLÁSTICOS)) {
            options.icon(BitmapDescriptorFactory.fromResource(R.drawable.pino_plastico));
        } else if (PropertiesUtils.getInscance().getTipo().equals(PropertiesUtils.TipoReciclado.VIDROS)) {
            options.icon(BitmapDescriptorFactory.fromResource(R.drawable.pino_vidro));
        }

        marker = mapa.addMarker(options);
    }
}